<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="80")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-if="item.model === 'insuranceName'" v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.name"
                  :key="index"
                ) {{ c_item.name }}
              Select(v-else-if="item.model === 'serverName'" v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.label"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'radio'")
              RadioGroup(type="button" v-model="formData[item.model]")
                Radio.display-inline-block(v-for="c_item of item.radioArr" :label="c_item.value") {{c_item.label}}
            template(v-if="item.type === 'intervalM'")
              DatePicker.pull-left.interval(v-model="formData[item.model1]" :placeholder="item.placeholder1" type="month" @on-change="(val, type) => handleChangeDatePicker(val, 'month', item.model1)")
              span.pl-10.pr-10.pull-left.line-h-32 --
              DatePicker.pull-left.interval(v-model="formData[item.model2]" :placeholder="item.placeholder2" type="month" @on-change="(val, type) => handleChangeDatePicker(val, 'month', item.model2)")
            template(v-if="item.type === 'intervalD'")
              DatePicker.pull-left.interval-date(v-model="formData[item.model1]" :placeholder="item.placeholder1" type="date" @on-change="(val, type) => handleChangeDatePicker(val, 'date', item.model1)")
              span.pl-10.pr-10.pull-left.line-h-32 --
              DatePicker.pull-left.interval-date(v-model="formData[item.model2]" :placeholder="item.placeholder2" type="date" @on-change="(val, type) => handleChangeDatePicker(val, 'date', item.model2)")
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable"
          @on-selection-change="handleOnSelectChange"
        )
          .clearfix.overflow-hidden(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
            .pull-right.table-title-btns
              Button.mr-10(type="info" @click="handleExport") 导出
          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )
          template(slot-scope="{ row, index }" slot="action")
            Button.mr-10(type="text" @click="handleGoDetail(row)") 明细
</template>

<script>
import { Common } from '@/utils/common.js'
import { ReceivableBillApi } from '@/api/ReceivableBill.api.js'
import { EnumApi } from '@/api/Enum.api.js'
import { CapitalFlowThead } from './CFtableHead.js'
import _cloneDeep from 'clone-deep'
import { EmployeesApi } from '@/api/Employees.api.js'

export default {
  name: '', // 银行到款管理页面

  components: {
  },

  data () {
    return {
      thead: _cloneDeep(CapitalFlowThead),
      tableData: [],
      formData: {
        companyName: ''
      },
      formItem: [
        { type: 'input', label: '客户名称：', placeholder: '请输入客户名称', prop: 'companyName', model: 'companyName', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6' },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-4 ivu-col-span-xxl-4' }
      ],
      rules: {},
      selectedEmployeeId: [], // 勾选的id集合,array格式
      selectedEmployeeArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10, // 当前页面展示条数
      },
      arrListDown: {}
    }
  },

  mounted () {
    this.query()
  },

  methods: {
    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await ReceivableBillApi.cashFlowList({ ...params, ...this.formData })
      console.log('银行到款res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
        this.tableComone.totalMoney = res.data.totalMoney
      } else {
        this.tableData = []
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.loading = false
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedEmployeeArr = value.map((item) => {
        return { id: item.id, name: item.name }
      })
      this.selectedEmployeeId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedEmployeeArr)
      console.log('列表勾选数组2', this.selectedEmployeeId)
    },

    // 导出资金流
    async handleExport () {
      var params = {
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await ReceivableBillApi.exportCashFlow({ ...params, ...this.formData })
      if (res.status === 200 && res.data.success) {
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 去往明细页面
    handleGoDetail (row) {
      this.$router.push({
        path: '/capitalFlowDetail',
        name: 'capitalFlowDetail',
        query: {
          companyID: row.companyID,
          companyName: row.companyName
        }
      })
    },
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
